package com.fr.data.util.function;

import com.fr.base.FRContext;
import com.fr.base.StringUtils;
import com.fr.base.core.list.IntList;
import com.fr.data.TableData;
import com.fr.data.TableDataException;
import com.fr.data.core.DataUtils;
import com.fr.report.core.DeathCycleException;
import com.fr.report.core.cal.SE;
import com.fr.report.core.cal.SheetExecuter;
import com.fr.report.core.cal.SynchronizedSEList;
import com.fr.report.script.Calculator;
import com.fr.report.script.SingleRowNameSpace;
import com.fr.report.script.core.FArray;
import com.fr.report.script.core.parser.ConditionalOrExpression;
import java.util.HashSet;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CurrentContextDataSetFunction extends DataSetFunction
{
  private static final CurrentContextDataSetFunction SC = new CurrentContextDataSetFunction();
  public static final Object CUR_DSNAME = new Object();

  public static CurrentContextDataSetFunction getInstance()
  {
    return SC;
  }

  public Object resolveFunction(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression, char paramChar)
  {
    if (paramArrayOfConditionalOrExpression.length == 0)
      return null;
    if (StringUtils.isBlank(paramString))
      return null;
    SheetExecuter localSheetExecuter = SynchronizedSEList.getSheetExecuter(Thread.currentThread());
    if (!(localSheetExecuter instanceof SE))
      return null;
    Calculator localCalculator = ((SE)localSheetExecuter).getCurrentCalculator();
    if (localCalculator == null)
      return null;
    List localList = (List)localCalculator.getAttribute(CUR_DSNAME);
    if (localList.contains(paramString))
      throw new DeathCycleException("Death cycle exists at calculating " + paramString);
    localList.add(paramString);
    try
    {
      int[] arrayOfInt;
      TableData localTableData = DataUtils.getTableData4Function(localCalculator, paramString);
      try
      {
        if ((localTableData == null) || (localTableData.getColumnCount() == 0) || (localTableData.getRowCount() == 0))
        {
          Object localObject1 = null;
          localList.remove(paramString);
          return localObject1;
        }
        arrayOfInt = IntList.range(localTableData.getRowCount());
      }
      catch (TableDataException localTableDataException)
      {
        FRContext.getLogger().log(Level.WARNING, localTableDataException.getMessage(), localTableDataException);
        arrayOfObject = null;
        localList.remove(paramString);
        return arrayOfObject;
      }
      SingleRowNameSpace localSingleRowNameSpace = new SingleRowNameSpace(localTableData, -1, null);
      localCalculator.pushNameSpace(localSingleRowNameSpace);
      if (paramArrayOfConditionalOrExpression.length > 1)
      {
        arrayOfInt = fn_source_filter(paramArrayOfConditionalOrExpression[1], arrayOfInt, localSingleRowNameSpace, localCalculator);
        if (arrayOfInt.length == 0)
        {
          arrayOfObject = null;
          return arrayOfObject;
        }
      }
      localCalculator.removeNameSpace(localSingleRowNameSpace);
      Object[] arrayOfObject = fn_cc_ex_result(paramArrayOfConditionalOrExpression[0], arrayOfInt, localTableData, localSingleRowNameSpace, localCalculator);
      if ('G' == paramChar)
      {
        localObject2 = new HashSet();
        for (int i = 0; i < arrayOfObject.length; ++i)
          ((HashSet)localObject2).add(arrayOfObject[i]);
        FArray localFArray = new FArray(((HashSet)localObject2).toArray(new Object[0]));
        return localFArray;
      }
      Object localObject2 = new FArray(arrayOfObject);
      return localObject2;
    }
    finally
    {
      localList.remove(paramString);
    }
  }

  public Object resolveXFunction(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression, char paramChar)
  {
    return resolveFunction(paramString, paramArrayOfConditionalOrExpression, paramChar);
  }

  public Object resolveColumn(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression)
  {
    return null;
  }

  public Object resolveColumnCount(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression)
  {
    return null;
  }

  public Object resolveColumnName(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression)
  {
    return null;
  }

  public Object resolveValue(String paramString, ConditionalOrExpression[] paramArrayOfConditionalOrExpression)
  {
    return null;
  }
}